var cartDAO = require('../models/cartDAO')
var formidable = require('formidable')
var path = require('path')
var cartController = {
    getAllCart: function (req, res) {
        console.log(11)
        var userId=req.user.userId 
        console.log('vkk',userId)
        cartDAO.getAllCart(userId, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '查询失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },
    delAllCart: function (req, res) {
        var cartID = req.query.cartID
        cartDAO.delAllCart(cartID, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '删除失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '删除成功' })
            }
        })
    },
    // 批量删除
    delAllCarts: function (req, res) {
        var cartID = req.query.cartID
        console.log('批量',cartID)
        cartDAO.delAllCarts(cartID, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '删除失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '删除成功' })
            }
        })
    },
    updateCartnum: function (req, res) {
        var cartID = req.query.cartID
        console.log(cartID)
        cartDAO.updateCartnum(cartID, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '修改失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '修改成功' })
            }
        })
    },
    updateCartsubnum: function (req, res) {
        var cartID = req.query.cartID
        console.log(cartID)
        cartDAO.updateCartsubnum(cartID, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '修改失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '修改成功' })
            }
        })
    },
    insertCart: function (req, res) {

        var discountID = req.body.discountID
        var tradeID = req.body.tradeID
        var type = req.body.type
        var userId = req.user.userId
        var money = req.body.money
        var num = 1
        var sum =req.body.money
        console.log(discountID)
        console.log(tradeID)
        console.log(type)
        console.log('ceshi',tradeID)
        cartDAO.insertCart([discountID,tradeID,type,userId,num,sum,money], function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '修改失败' })
            } else {
                console.log(results)
                res.json({ code: 200, data: results, msg: '修改成功' })
            }
        })
    },
    getTypecart: function (req, res) {

        var type = req.query.type
        var userId = req.user.userId
        var discountID = req.query.discountID
        var tradeID=req.query.tradeID
        console.log(type)
        console.log('type1',type)
        console.log('type2',discountID)
       
        console.log('type4',tradeID)
        cartDAO.getTypecart([type, userId, discountID,tradeID], function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '查询失败' })
            } else {
                console.log('zhi', results)
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },
    getcartID: function (req, res) {
       var cartID=req.query.cartID
      
       console.log('zhi', cartID)
        cartDAO.getcartID(cartID, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '查询失败' })
            } else {
                console.log('测试')
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },


}
module.exports = cartController